package com.cskaoyan.controller.promote;

import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.promote_ad.BaseData;
import com.cskaoyan.service.promote.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * TODO
 *
 * @author：23117
 * @date: 2021/12/2
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {
    @Autowired
    CouponService couponService;

    @RequestMapping("list")
    public BaseRespVo list(Integer page,Integer limit){
        BaseData data = couponService.wxList(page,limit);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("mylist")
    public BaseRespVo mylist(Integer page,Integer limit){
        BaseData data =couponService.wxMyList(page,limit);
        return BaseRespVo.ok(data);
    }

    @PostMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map map, HttpSession httpSession){
        //处理获取 请求参数
        Integer userid = (Integer) httpSession.getAttribute("uid");
        String code = (String) map.get("code");

        //调用service层
        int data = couponService.wxExchange(code,userid);

        //返回响应
        if (data==400){
            return BaseRespVo.fail("兑换码错误");
        }else if (data==300){
            return BaseRespVo.fail("兑换码已领取");
        }
        return BaseRespVo.manual(0,"领取成功",null);
    }

    @RequestMapping("selectlist")
    //参数的作用有待商榷cartID，grouponRulesId
    public BaseRespVo selectList(Integer cartID,Integer grouponRulesId,HttpSession httpSession){
        Integer userid = (Integer) httpSession.getAttribute("uid");

        BaseData data = couponService.selectList(cartID,grouponRulesId,userid);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("receive")
    public BaseRespVo receive(@RequestBody Map map, HttpSession httpSession){
        //处理获取 请求参数
        Integer userid = (Integer) httpSession.getAttribute("uid");
        Integer couponId = (Integer) map.get("couponId");
        if (userid == null) return BaseRespVo.manual(501, "请登录", null);

        //调用service层
        int data = couponService.receive(couponId,userid);

        //返回响应
        if (data==300){
            return BaseRespVo.fail("优惠券已经领取");
        }
        return  BaseRespVo.manual(0,"领取成功",null);
    }

}




